<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<#include "head.ftl" />
</head>
<body>
	<div class="body-box">
		<div class="rhead">
			<div class="rpos">当前位置: 角色权限 - 编辑</div>
			<form class="ropt">
				<input type="button" value="返回"
					onclick="location.href='${st.domain}/role/list'" />
			</form>
			<div class="clear"></div>
		</div>
		<form method="post" action="${st.domain}/role/addOrEditDo" id="form">
			<table width="100%" class="pn-ftable" cellpadding="2" cellspacing="1"
				border="0">
				<tr>
					<td width="10%" class="pn-flabel pn-flabel-h"><span
						class="pn-frequired">*</span>角色名称:</td>
					<td colspan="1" class="pn-fcontent"><input
						type="text" value="${role.name}" name="name" class="required"
						size="25" maxlength="150" />
					</td>
				</tr>
				<tr>
					<td width="10%" class="pn-flabel pn-flabel-h"><strong>权限:</strong></td>
					<td colspan="1" class="pn-flabel pn-flabel-h">&nbsp;
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="goods_all" value="goods:*">商品库</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="goods_view" value="goods:view">查看
						<input type="checkbox" id="goods_addOrEdit" value="goods:addOrEdit">添加/编辑
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="cook_all" value="cook:*">菜谱</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="cook_view" value="cook:view">查看
						<input type="checkbox" id="cook_addOrEdit" value="cook:addOrEdit">添加/编辑
						<input type="checkbox" id="cook_del" value="cook:del">删除
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="store_all" value="store:*">店铺</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="store_view" value="store:view">查看
						<input type="checkbox" id="store_addOrEdit" value="store:addOrEdit">添加/编辑
						<input type="checkbox" id="store_addMember" value="store:addMember">设定管理者
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="article_all" value="article:*">内容管理</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="article_view" value="article:view">查看
						<input type="checkbox" id="article_addOrEdit" value="article:addOrEdit">添加/编辑
						<input type="checkbox" id="article_mkHtml" value="article:mkHtml">生成HTML
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="category_all" value="category:*">分类管理</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="category_addOrEdit" value="category:addOrEdit">添加/编辑
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="member_all" value="member:*">注册用户</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="member_view" value="member:view">查看
						<input type="checkbox" id="member_edit" value="member:edit">编辑
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="notice_all" value="notice:*">系统通知</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="notice_view" value="notice:view">查看
						<input type="checkbox" id="notice_add" value="notice:add">发布/修改
						<input type="checkbox" id="notice_del" value="notice:del">删除
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="prizeWin_all" value="prizeWin:*">用户奖品</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="prizeWin_view" value="prizeWin:view">查看
						<input type="checkbox" id="prizeWin_convert" value="prizeWin:convert">兑换
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="prize_all" value="prize:*">奖品设置</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="prize_view" value="prize:view">查看
						<input type="checkbox" id="prize_set" value="prize:set">设置
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="setting_all" value="setting:*">配置参数</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="setting_view" value="setting:view">查看
						<input type="checkbox" id="setting_edit" value="setting:edit">编辑
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="region_all" value="region:*">地区管理</td>
					<td colspan="1" class="pn-fcontent">
						&nbsp;
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="admin_all" value="admin:*">管理员</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="admin_view" value="admin:view">查看
						<input type="checkbox" id="admin_addOrEdit" value="admin:addOrEdit">添加/编辑
						<input type="checkbox" id="admin_role" value="admin:role">设置角色
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><input type="checkbox" id="role_all" value="role:*">角色权限</td>
					<td colspan="1" class="pn-fcontent">
						<input type="checkbox" id="role_view" value="role:view">查看
						<input type="checkbox" id="role_addOrEdit" value="role:addOrEdit">添加/编辑
					</td>
				</tr>
				<tr>
					<td colspan="4" class="pn-fbutton">
						<input type="hidden" id="perms" name="perms" value="${role.perms}" />
						<input type="hidden" name="roleId" value="${role.roleId}" />
						<input type="hidden" name="id" value="1" /> <input type="button" id="bt_submit" value="提交" /> &nbsp; 
						<input type="reset" value="重置" />
					</td>
				</tr>
			</table>
		</form>
	</div>
	<script>
	var addOrEditPath = g_domain + "/role/addOrEditDo/json";
	var listPath = g_domain + "/role/list";
	
	$(":checkbox").click(function() {
		var id = $(this).attr("id");
		var type = (id.split("_"))[0];
		if (id.indexOf("_all") != -1 ) {
			// 选中父项,则清空所有子项选中
			if ($(this).prop("checked")) {
				$("[id^='" + type + "_']").prop("checked", false);
				$(this).prop("checked", true);
			}
		} else {
			// 选中所有子项时,则选中父项并清空子项
			if ($("[id^='" + type + "_'][id!='" + type + "_all']:checked").length == $("[id^='" + type + "_'][id!='" + type + "_all']").length) {
				$("#" + type + "_all").prop("checked", true);
				$("[id^='" + type + "_'][id!='" + type + "_all']").prop("checked", false);
				
			// 有子项选中时,则去掉父项选中
			} else if ($("[id^='" + type + "_'][id!='" + type + "_all']:checked").length > 0) {
				$("#" + type + "_all").prop("checked", false);
			}
		}
	});
	
	// 默认选中
	var ids = $("#perms").val().replace(/\*/g, "all").replace(/\:/g, "_").split(",");
	for (var i = 0; i < ids.length; i++) {
		$("#" + ids[i]).prop("checked", true);
	}
	
	// 事件:提交
	$("#bt_submit").click(function() {
		var perms = $(":checkbox:checked").map(function() {
			return $(this).val();
		}).get().join(",");
		$("#perms").val(perms);
		$.post(addOrEditPath, $("#form").serialize(), function(data) {
			alert(data.message);
			if (data.status !== 0) {
				return;
			}
			location.href = listPath; 
		});
	});
	</script>
</body>
</html>